package com.taotao.portal.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.taotao.common.util.JsonUtils;
import com.taotao.content.service.ContentService;

import com.taotao.pojo.TbContent;
import com.taotao.portal.pojo.Ad1Node;

/**
 * 展示首页
 * @title PageController
 * <p>description</p>
 * <p>company: www.itheima.com</p>
 * @author 天火
 * @version 1.0
 */

@Controller
public class PageController {
	
	@Autowired
	private ContentService contentService;
	
	@Value("${BIGAD_CATEGORY_ID}")
	private String BIGAD_CATEGORY_ID;
	
	@Value("${BIGAD_HEIGHT}")
	private String BIGAD_HEIGHT;
	
	@Value("${BIGAD_HEIGHT_B}")
	private String BIGAD_HEIGHT_B;
	
	@Value("${BIGAD_WIDTH}")
	private String BIGAD_WIDTG;
	
	@Value("${BIGAD_WIDTH_B}")
	private String BIGAD_WIDTG_B;

	@RequestMapping("/index")
	public String showIndex(Model model) {
		//引入服务
		//注入服务
		//CESHI
		//添加业务逻辑根据内容分类的id 来查询内容列表//
		Long categoryId = Long.parseLong(BIGAD_CATEGORY_ID);
		List<TbContent> contents = contentService.getContentListByCatgoryId(categoryId);
		//转成自定义的pojo Ad1Node列表
		List<Ad1Node> nodes = new ArrayList<>() ;
		for(TbContent tbContent :contents) {
			Ad1Node node = new Ad1Node();
			node.setAlt(tbContent.getSubTitle());
			node.setHeight(BIGAD_HEIGHT);
			node.setHeightB(BIGAD_HEIGHT_B);
			node.setHref(tbContent.getUrl());
			node.setSrc(tbContent.getPic());
			node.setSrcB(tbContent.getPic2());
			node.setWidth(BIGAD_WIDTG);
			node.setWidthB(BIGAD_WIDTG_B);
			nodes.add(node);
			
		}
		//传递数据给jsp
		model.addAttribute("ad1", JsonUtils.objectToJson(nodes));
		return "index";
	}
	
}
